<template>
  <div>
    <FormPanel ref="from" :list="formList" @formChange="changeHandle" :isSubmitBtn="false" :collapse="false" labelWidth="110" />
    <div style="height:47px;"></div>
    <BaseDialog
      :visible.sync="isSelectCar"
      :title="'车辆查询'"
      destroyOnClose
      :containerStyle="{height: 'calc(100% - 60px)', overflow: 'auto', paddingBottom: '60px'}"
    >
      <vinSelect  :ownerCode="dealerCode" @close="carCloseHandler" />
    </BaseDialog>
    <BaseDialog
      :visible.sync="issearch"
      :title="'选择经销商'"
      destroyOnClose
      :containerStyle="{height: 'calc(100% - 60px)', overflow: 'auto', paddingBottom: '60px'}"
    >
      <ownerSeachers  @close="ownercloseHandler" />
    </BaseDialog>
    <div
      :style="{
        position: 'absolute',
        left: 0,
        bottom: 0,
        right: 0,
        zIndex: 9,
        borderTop: '1px solid #e9e9e9',
        padding: '10px 20px',
        background: '#fff',
        textAlign: 'right'
      }"
    >
      <el-button @click="closeDrawer(1)">关 闭</el-button>
      <multiuse-button type="primary" :click="closeDrawer.bind(this, 2)">提 交</multiuse-button>
      <multiuse-button type="primary" :click="closeDrawer.bind(this, 3)">保 存</multiuse-button>
    </div>
  </div>
</template>
<script>
import {getDetail,updatePlateApply} from '@/api/omSale/PurchaseReturn/ReturnAudit/index.js';
import { dictionary } from '@/utils/dictMixin';
import ownerSeachers from './ownerSeachers'
import vinSelect from './vinSelect';
import moment from 'moment'
import Cookie from 'js-cookie'
export default {
  data(){
    this.saveData=null;
    return{
      formList:this.createdFrom(),
      issearch:false,
      isSelectCar:false,
      carData:{},
      formData:{},
      dealerCode:'',
      userName:'',
      userId:'',
    }
  },
  mixins:[dictionary],
  components:{
    vinSelect,
    ownerSeachers
  },
  props:['rowData'],
  created(){
    this.getDetail(this.rowData)
  },
  methods:{
    createdFrom(){
      return [
        {
          type: 'INPUT',
          label: '退货单号',
          fieldName: 'returnNo',
          placeholder: '请选择',
          disabled:true,

        },
        {
          type: 'INPUT',
          label: '经销商',
          fieldName: 'dealerName',
          placeholder: '请输入',
          readonly:true,
          unitRender: () => {
            return (
              <div>
                <el-button icon="el-icon-search" onClick={() => this.seacherFun(1)}></el-button>
              </div>
            );
          },
          rules: [{ required: true, message: '请选择', trigger: 'blur' }],
        },
        {
          type: 'DATE',
          label: '申请日期',
          fieldName: 'applyDate',
          placeholder: '请输入',
          dateType:'exactdate',
          disabled:true,
          initialValue:moment().format('YYYY-MM-DD')
        },
        {
          type: 'SELECT',
          label: '退车原因',
          fieldName: 'returnReason',
          placeholder: '请输入',
          itemList:this.createDictList('6056'),
          rules: [{ required: true, message: '请选择', trigger: 'change' }],
        },
        {
          type: 'INPUT',
          label: '退货人',
          fieldName: 'applyPersonName',
          placeholder: '请输入',
          disabled:true,
        },

        {
          type: 'INPUT',
          label: '店内联系人',
          fieldName: 'linkMan',
          placeholder: '请输入',
        },
        {
          type: 'INPUT',
          label: 'VIN',
          fieldName: 'vin',
          placeholder: '请输入',
          readonly:true,
          unitRender: () => {
            return (
              <div>
                <el-button icon="el-icon-search" onClick={() => this.seacherFun(2)}></el-button>
              </div>
            );
          },
          rules: [{ required: true, message: '请选择', trigger: 'blur' }],
        },

        {
          type: 'INPUT',
          label: '销售订单号',
          fieldName: 'doNo',
          placeholder: '请输入',
          disabled:true,
        },
        {
          type: 'INPUT',
          label: '联系方式',
          fieldName: 'linkPhone',
          placeholder: '请输入',
        },
        {
          type: 'INPUT',
          label: '产品名称',
          fieldName: 'productName',
          placeholder: '请输入',
          disabled:true,
        },
        {
          type: 'INPUT',
          label: '原价格',
          fieldName: 'afterDiscountRebateAmount',
          placeholder: '请输入',
          disabled:true,
        },
        {
          type: 'INPUT',
          label: '原发票号',
          fieldName: 'goldenInvoiceNo',
          placeholder: '请输入',
          disabled:true,
        },
        {
          type: 'INPUT',
          label: '原税率',
          fieldName: 'rate',
          placeholder: '请输入',
          disabled:true,
        },
        {
          type: 'INPUT_NUMBER',
          label: '退车金额',
          fieldName: 'returnAmount',
          placeholder: '请输入',
          disabled:true,
          max:1000000000
        },
        {
          type: 'SELECT',
          label: '店端车辆状态',
          fieldName: 'vehicleStatus',
          placeholder: '请输入',
          disabled:true,
          itemList:this.createDictList('6040')
        },
        {
          type: 'TEXT_AREA',
          label: '退车原因说明',
          fieldName: 'returnReasonRemark',
          placeholder: '请输入',
          selfCols:3
        },
        {
          type: 'UPLOAD_FILE',
          label: '附件上传',
          fieldName: 'pos',
          placeholder: '请输入',
          initialValue:[],
          offsetRightCols:2,
          upload: {
            actionUrl: '/api/file/oss/upload',
            limit: 1000,
            tipText: '最多上传1000个附件',
          },
        },
        {
          type: 'TEXT_AREA',
          label: '审核意见',
          fieldName: 'returnRemark',
          placeholder: '请输入',
          disabled:true,
          hidden:true,
          selfCols:3
        },

      ]
    },
    seacherFun(type){
      if(type==1){
        this.issearch=true;
      }else{
        this.isSelectCar=true;
        // if(this.dealerCode){
          
        // }else{
        //    this.$notify({
        //     title: '警告',
        //     message: '请先选择经销商',
        //     type: 'warning'
        //   });
        //   return;
        // }

      }
    },
    ownercloseHandler(val){
      if(val){
        console.log(val)
        this.formList.map(x=>{
          if(x.fieldName=='dealerName'){
            x.initialValue=val.dealerName;
            this.dealerCode=val.dealerCode
          }
          if(x.fieldName=='linkMan'){
            x.initialValue=val.emergencyContract;
          }
          if(x.fieldName=='linkPhone'){
            x.initialValue=val.emergencyContractPhone
          }
          if(x.fieldName=='vin'){
            x.initialValue='';
          }
          if(x.fieldName=='doNo'){
            x.initialValue='';
          }
          if(x.fieldName=='productName'){
            x.initialValue='';
          }
          if(x.fieldName=='afterDiscountRebateAmount'){
            x.initialValue='';
          }
          if(x.fieldName=='goldenInvoiceNo'){
            x.initialValue='';
          }
          if(x.fieldName=='rate'){
            x.initialValue='';
          }
          if(x.fieldName=='returnAmount'){
            x.initialValue=''
          }
        })
      }
      this.issearch=false;
    },
    changeHandle(val){
      this.saveData=val;
    },
    async closeDrawer(type){
       if (type == 1) {
        this.$emit('close');
      } else if (type == 2) {
        this.$refs.from.SUBMIT_FORM();
        if (this.saveData) {
          let json = JSON.parse(JSON.stringify(this.saveData));
          if(json.pos&&json.pos.length>0){
            json.pos.map(x=>{
              x.fileName=x.name;
              x.fileAddress=x.url
            })
          }
          json.reportPersonId=this.userId;
          json.id=this.rowData.id;
          json.dealerCode=this.dealerCode;
          json.returnStatus=60551002;
          let res = await updatePlateApply(json);
          if (res.resultCode == 200) {
            this.$notify({
              title: '成功',
              message: '提交成功',
              type: 'success'
            });
            this.$emit('close', json);
          } else {
            this.saveData = null;
          }
        }
      }else{
        this.$refs.from.SUBMIT_FORM();
        if (this.saveData) {
          let json = JSON.parse(JSON.stringify(this.saveData));
          if(json.pos&&json.pos.length>0){
            json.pos.map(x=>{
              x.fileName=x.name;
              x.fileAddress=x.url
            })
          }
          json.reportPersonId=this.userId;
          json.id=this.rowData.id;
          json.dealerCode=this.dealerCode;
          if(this.rowData.returnStatus==60551004){
            json.returnStatus=60551004;
          }else{
            json.returnStatus=60551001;
          }

          let res = await updatePlateApply(json);
          if (res.resultCode == 200) {
            this.$notify({
              title: '成功',
              message: '保存成功',
              type: 'success'
            });
            this.$emit('close', json);
          } else {
            this.saveData = null;
          }
        }
      }
    },
    carCloseHandler(val){
      if(val){
        this.formList.map(x=>{
          if(x.fieldName=='vin'){
            x.initialValue=val.vin;
          }
          if(x.fieldName=='doNo'){
            x.initialValue=val.doNo;
          }
          if(x.fieldName=='productName'){
            x.initialValue=val.productName;
          }
          if(x.fieldName=='afterDiscountRebateAmount'){
            x.initialValue=val.afterDiscountRebateAmount;
          }
          if(x.fieldName=='goldenInvoiceNo'){
            x.initialValue=val.goldenInvoiceNo;
          }
          if(x.fieldName=='rate'){
            x.initialValue=val.rate;
          }
          if(x.fieldName=='returnAmount'){
            x.initialValue=val.afterDiscountRebateAmount
          }
          if(x.fieldName=='dealerName'){
            x.initialValue=val.dealerName;
            this.dealerCode=val.dealerCode
          }
          if (x.fieldName == 'linkMan') {
            x.initialValue = val.linkMan;
          }
          if (x.fieldName == 'linkPhone') {
            x.initialValue = val.linkPhone;
          }
        })
        this.carData=val;
      };
      this.isSelectCar=false;
    },
    async getDetail(data){
      let res=await getDetail({id:data.id})
      if(res.data.pos&&res.data.pos.length>0){
        res.data.pos.map(x=>{
          x.name=x.fileName
          x.url=x.fileAddress
        })
      }

      for(let i in res.data){
        this.formList.map(x=>{
          if(x.fieldName==i){
            x.initialValue=res.data[i]
          }
        })
      }
      this.dealerCode=res.data.dealerCode;
      this.userId=res.data.reportPersonId;
      this.userName=res.data.reportPersonName
      if(res.data.returnStatus==60551004){
        this.formList.find(x=>x.fieldName=='returnRemark').hidden=false;
      }
    }
  }
}
</script>
